//
//  JQGraphicVerificationView.m
//  JQLive
//
//  Created by Bruce on 2020/8/20.
//  Copyright © 2020 mlkq. All rights reserved.
//

#import "JQGraphicVerificationView.h"
@interface JQGraphicVerificationView()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *actionBgView;

@property (nonatomic, strong) UITextField *graphicVerificationTextField;// 图形验证输入框
@property (nonatomic, strong) UIButton *graphicVerificationEnterBtn;// 图形验证 确认按钮
@property (nonatomic, strong) UIImageView *graphicVerificationImageV;// 图形验证图片
@property (nonatomic, strong) CAGradientLayer *graphicVerificationGl;
@end

@implementation JQGraphicVerificationView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@synthesize block;

- (void)initView{
//    self.backgroundColor = [UIColor clearColor];
    [self addGraphicVerification];
}
// 加入图形验证
- (void)addGraphicVerification{
    UIView *actionBgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [[UIApplication sharedApplication].keyWindow addSubview:actionBgView];
//    [self.view addSubview:actionBgView];
    self.actionBgView = actionBgView;
    MY_WEAK;
    [self.actionBgView my_addTapGes:^{
        
        MY_STRONG;
        [strongSelf.actionBgView endEditing:YES];
//        [strongSelf.actionBgView removeFromSuperview];
//        [strongSelf.actionBgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }];
    actionBgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.289];
    
    
    // 手机验证登录
    UIView *graphicVerificationView = [[UIView alloc] init];
    [actionBgView addSubview:graphicVerificationView];
    [graphicVerificationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(actionBgView);
        make.height.mas_equalTo(177);
        make.width.mas_equalTo(330);
    }];
    graphicVerificationView.backgroundColor = [UIColor whiteColor];
    [graphicVerificationView layoutIfNeeded];
    [graphicVerificationView round:20 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight];
    graphicVerificationView.hidden = NO;
//    self.graphicVerificationView = graphicVerificationView;
    
    [self setUIWithgraphicVerificationView:graphicVerificationView];
    
}

- (void)setUIWithgraphicVerificationView:(UIView *)graphicVerificationView{
    MY_WEAK;
    
    UIButton *closeBtn = [[UIButton alloc] init];
    [graphicVerificationView addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(graphicVerificationView.mas_top).offset(16);
        make.right.mas_equalTo(graphicVerificationView.mas_right).offset(-16);
        make.size.mas_equalTo(CGSizeMake(28, 28));
    }];
    [closeBtn setImage:[UIImage svgImageNamed:@"right_close" size:CGSizeMake(15, 15)] forState:UIControlStateNormal];
    [closeBtn addTapBlock:^(UIButton * _Nonnull btn) {
        [self.actionBgView removeFromSuperview];
        [self.actionBgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }];
    
    
    UILabel *graphicVerificationLb = [[UILabel alloc] init];
    [graphicVerificationView addSubview:graphicVerificationLb];
    [graphicVerificationLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(graphicVerificationView.mas_top).offset(22);
        make.left.mas_equalTo(graphicVerificationView.mas_left).offset(16);
        make.height.mas_equalTo(@(17));
    }];
    graphicVerificationLb.textColor = kColorWithHex(0x010192);
    graphicVerificationLb.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
    graphicVerificationLb.text = @"图形验证码识别";
    
    UITextField *graphicVerificationTextField = [[UITextField alloc] init];
    [graphicVerificationView addSubview:graphicVerificationTextField];
    self.graphicVerificationTextField = graphicVerificationTextField;
//    graphicVerificationTextField.delegate = self;
    graphicVerificationTextField.keyboardType = UIKeyboardTypeDefault;
    graphicVerificationTextField.returnKeyType = UIReturnKeyDone;
    graphicVerificationTextField.delegate = self;
    [graphicVerificationTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(graphicVerificationLb.mas_bottom).offset(9);
        make.left.mas_equalTo(graphicVerificationView.mas_left).offset(16);
        make.right.mas_equalTo(graphicVerificationView.mas_right).offset(-16);
        make.height.mas_equalTo(@(40));
    }];
    [self setTextField:graphicVerificationTextField placeholder:@"请输入右边图形验证码"];
//    [self addPwdIsSecretBtnWith:graphicVerificationTextField];
    // 加入验证码
    [self addGraphicVerificationTextField:graphicVerificationTextField];
    
    UIButton *graphicVerificationEnterBtn = [[UIButton alloc] init];
    [graphicVerificationView addSubview:graphicVerificationEnterBtn];
    self.graphicVerificationEnterBtn = graphicVerificationEnterBtn;
    [graphicVerificationEnterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(graphicVerificationTextField.mas_bottom).offset(20);
        make.left.right.equalTo(graphicVerificationTextField);
        make.height.mas_equalTo(@(40));
    }];
    [graphicVerificationEnterBtn setTitle:@"确认" forState:UIControlStateNormal];
    [graphicVerificationEnterBtn setTitleColor:[UIColor whiteColor]  forState:UIControlStateNormal];
    graphicVerificationEnterBtn.titleLabel.font = [UIFont systemFontOfSize:15];

    [graphicVerificationEnterBtn layoutIfNeeded];
    [graphicVerificationEnterBtn round:20 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight];
    [graphicVerificationEnterBtn setBackgroundColor:kColorWithHex(0xC8CCE1)];
    
    [graphicVerificationEnterBtn addTapBlock:^(UIButton * _Nonnull btn) {
        // 跳转 数据传递
        MY_STRONG;
        if (strongSelf.block) {
            strongSelf.block(self.graphicVerificationTextField.text);
        }
        
        [self.actionBgView removeFromSuperview];
        [self.actionBgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
        // 跳转
//        JQUserChangePwdVC *changePwdVC = [[JQUserChangePwdVC alloc] init];
//        changePwdVC.title = @"修改密码";
//        [self.navigationController pushViewController:changePwdVC animated:YES];
    }];
}
- (void)addGraphicVerificationTextField:(UITextField *)graphicVerificationTextField{
//    MY_WEAK;
    UIView *rightBackGroundView = [[UIView alloc] init];
    [graphicVerificationTextField addSubview: rightBackGroundView];
    [rightBackGroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(graphicVerificationTextField);
        make.width.mas_equalTo(@(88));
    }];
       
    UIImageView *graphicVerificationImageV = [[UIImageView alloc] init];
    [rightBackGroundView addSubview:graphicVerificationImageV];
    [graphicVerificationImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.mas_equalTo(rightBackGroundView);
        make.right.mas_equalTo(rightBackGroundView.mas_right).offset(-8);
    }];
    self.graphicVerificationImageV = graphicVerificationImageV;
    
    [graphicVerificationImageV sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage qmui_imageWithColor:[UIColor grayColor]]];
}
// 底部登录按钮的 渐变效果
- (void)setGradientLayerWithView:(UIView *)view{
    // gradient
    if (self.graphicVerificationGl) {
        [self.graphicVerificationGl removeFromSuperlayer];
        self.graphicVerificationGl = nil;
    }
    
    CAGradientLayer *gl = [CAGradientLayer layer];
    self.graphicVerificationGl = gl;
    gl.frame = view.bounds;//CGRectMake(38,476,299,40);
    gl.startPoint = CGPointMake(1, 0.5);
    gl.endPoint = CGPointMake(0, 0.5);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:49/255.0 green:0/255.0 blue:186/255.0 alpha:1.0].CGColor, (__bridge id)[UIColor colorWithRed:0/255.0 green:0/255.0 blue:229/255.0 alpha:1.0].CGColor];
    gl.locations = @[@(0), @(1.0f)];
    
    [view.layer addSublayer:gl];
    [view.layer insertSublayer:gl atIndex:0];
    
//    [gl removeFromSuperlayer]
}
// textfield 自定义UI
- (void)setTextField:(UITextField *)textField placeholder:(NSString *)placeholder {
    textField.placeholder = [NSString
                             stringWithFormat:@"   %@", placeholder];
    textField.backgroundColor = kColorWithHex(0xF2F4FC);
    [textField layoutIfNeeded];
    [textField round:5 RectCorners:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight];
    
    [textField setValue:[NSNumber numberWithInt:10] forKey:@"paddingLeft"];
//    graphicVerificationTextField.tintColor = kColorWithHex(0x010192);
    textField.textColor = kColorWithHex(0x010192);
    textField.font = [UIFont systemFontOfSize:14];
    if (@available(iOS 13.0, *)){
        textField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:textField.placeholder attributes:@{NSForegroundColorAttributeName:[kColorWithHex(0x010192) colorWithAlphaComponent:0.3], NSFontAttributeName:[UIFont systemFontOfSize:13]}];
    }else{
        [textField setValue:[kColorWithHex(0x010192) colorWithAlphaComponent:0.3] forKeyPath:@"_placeholderLabel.textColor"];
        [textField setValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];
    }
}
#pragma mark - UITextDeleagte
- (void)textFieldDidEndEditing:(UITextField *)textField{
    [self textFieldDidChangeSelectionComond:textField];
}
// iOS13 系统才走的方法
- (void)textFieldDidChangeSelection:(UITextField *)textField{
    [self textFieldDidChangeSelectionComond:textField];
}
// 代理来监听textField输入字符变化
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (@available(iOS 13.0, *)) {
        
    }else{
        [self textFieldDidChangeSelectionComond:textField];
    }
    return YES;
}
// 监听textField 公用方法
- (void)textFieldDidChangeSelectionComond:(UITextField *)textField{
    if (textField == self.graphicVerificationTextField){
        if (textField.text.length > 5) {
            textField.text = [textField.text substringToIndex:5];
        }
    }
    if ((textField == self.graphicVerificationTextField) && (textField.text.length == 5)) {
        [self setGradientLayerWithView:self.graphicVerificationEnterBtn];
        self.graphicVerificationEnterBtn.enabled = YES;
    }else{
        [self.graphicVerificationEnterBtn setBackgroundColor:kColorWithHex(0xC8CCE1)];
        if (self.graphicVerificationGl) {
            [self.graphicVerificationGl removeFromSuperlayer];
            self.graphicVerificationGl = nil;
        }
        self.graphicVerificationEnterBtn.enabled = NO;
    }
}
@end
